
package br.com.duduso.conferencia_claro_desktop.screens;

import br.com.duduso.conferencia_claro_desktop.components.Mensagens;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author duduso
 */
public class TelaLogin extends javax.swing.JFrame {

    private static final String LOGIN = "elaine.oliveira";
    private static final String PASSWORD = "2212@du";
    private KeyAdapter keyAdapter;

    public TelaLogin() {
            initComponents();
            initKeyListener();
            setLocationRelativeTo(null);
            setResizable(false);
            this.btEntrar.addKeyListener(keyAdapter);
            this.btCancelar.addKeyListener(keyAdapter);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnLogin = new javax.swing.JPanel();
        lbUsuario = new javax.swing.JLabel();
        tfUsuario = new javax.swing.JTextField();
        lbSenha = new javax.swing.JLabel();
        pfSenha = new javax.swing.JPasswordField();
        btEntrar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("PÁGINA DE LOGIN DO SISTEMA"); // NOI18N

        pnLogin.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(93, 72, 72), new java.awt.Color(179, 27, 27), null, new java.awt.Color(182, 61, 61)));
        pnLogin.setFont(new java.awt.Font("Arial", 0, 12));
        pnLogin.setName("pnLogin"); // NOI18N

        lbUsuario.setFont(new java.awt.Font("Arial", 1, 12));
        lbUsuario.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbUsuario.setText("Usuário: ");
        lbUsuario.setName("lbUsuario"); // NOI18N

        tfUsuario.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        tfUsuario.setName("tfUsuario"); // NOI18N

        lbSenha.setFont(new java.awt.Font("Arial", 1, 12));
        lbSenha.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbSenha.setText("Senha: ");
        lbSenha.setName("lbSenha"); // NOI18N

        pfSenha.setFont(new java.awt.Font("Arial", 0, 12));
        pfSenha.setName("pfSenha"); // NOI18N

        btEntrar.setFont(new java.awt.Font("Arial", 1, 12));
        btEntrar.setText("Entrar");
        btEntrar.setName("btEntrar"); // NOI18N
        btEntrar.setPreferredSize(new java.awt.Dimension(63, 27));
        btEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEntrarActionPerformed(evt);
            }
        });

        btCancelar.setFont(new java.awt.Font("Arial", 1, 12));
        btCancelar.setText("Cancelar");
        btCancelar.setName("btCancelar"); // NOI18N
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnLoginLayout = new javax.swing.GroupLayout(pnLogin);
        pnLogin.setLayout(pnLoginLayout);
        pnLoginLayout.setHorizontalGroup(
            pnLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnLoginLayout.createSequentialGroup()
                .addGroup(pnLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnLoginLayout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(pnLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lbSenha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lbUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tfUsuario)
                            .addComponent(pfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(pnLoginLayout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addComponent(btEntrar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(60, Short.MAX_VALUE))
        );
        pnLoginLayout.setVerticalGroup(
            pnLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnLoginLayout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(pnLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbUsuario)
                    .addComponent(tfUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbSenha)
                    .addComponent(pfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btEntrar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btCancelar))
                .addContainerGap(39, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(275, 275, 275)
                .addComponent(pnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(325, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(164, 164, 164)
                .addComponent(pnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(214, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEntrarActionPerformed
            acessarAplicacao();
    }//GEN-LAST:event_btEntrarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
            dispose();
    }//GEN-LAST:event_btCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btEntrar;
    private javax.swing.JLabel lbSenha;
    private javax.swing.JLabel lbUsuario;
    private javax.swing.JPasswordField pfSenha;
    private javax.swing.JPanel pnLogin;
    private javax.swing.JTextField tfUsuario;
    // End of variables declaration//GEN-END:variables

    public void acessarAplicacao(){
            String usuario = tfUsuario.getText();
            String senha = new String(pfSenha.getPassword());
            if(StringUtils.isBlank(usuario)){
                    Mensagens.getInstance().exibirMensagemErro(null, "usuarioNaoInformado");
                    return;
            }
            if(StringUtils.isBlank(senha)){
                    Mensagens.getInstance().exibirMensagemErro(null, "senhaNaoInformada");
                    return;
            }
            if(!usuario.toLowerCase().equals(LOGIN)){
                    Mensagens.getInstance().exibirMensagemErro(null, "usuarioInvalido");
                    return;
            }
            if(!senha.equals(PASSWORD)){
                    Mensagens.getInstance().exibirMensagemErro(null, "senhaInvalida");
                    return;
            }
            dispose();
            new GUIPrincipal().setVisible(true);
    }

    private void initKeyListener(){
            this.keyAdapter = new KeyAdapter() {
                    @Override
                    public void keyPressed(KeyEvent event) {
                            if(event.getKeyCode() == KeyEvent.VK_ENTER){
                                    if(event.getSource() == btEntrar){
                                            acessarAplicacao();
                                    }else{
                                            dispose();
                                    }
                            }
                    }
            };
    }

}
